//
//  FollowMoveToAction.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-7-22.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "FollowMoveToAction.h"
#import "Camera.h"

@implementation FollowMoveToAction

+(id) actionWithDuration:(ccTime)duration followTarget:(CCNode*)followTarget
{
    return [[[self alloc] initWithDuration:duration followTarget:followTarget ] autorelease];
}

-(id) initWithDuration:(ccTime)duration followTarget:(CCNode*)followTarget
{
    if((self=[super initWithDuration: duration]))
    {
        followTarget_ = [followTarget retain];
    }
		
	return self;
}

-(id) copyWithZone: (NSZone*) zone
{
	CCAction *copy = [[[self class] allocWithZone: zone] initWithDuration: [self duration] followTarget: followTarget_];
	return copy;
}

-(void) startWithTarget:(CCNode *)aTarget
{
    NSAssert([aTarget isKindOfClass:[Camera class]], @"%@: not a Camera!", aTarget);
	[super startWithTarget:aTarget];
    Camera* camera = (Camera*)target_;
	startPosition = ccp(camera.centerX, camera.centerY);
}

-(void) update: (ccTime) t
{	
    Camera* camera = (Camera*)target_;
    delta = ccpSub(followTarget_.position, startPosition);
	camera.centerX = startPosition.x + delta.x * t;
    camera.centerY = startPosition.y + delta.y * t;
}

@end
